package com.erytech.linktalk.service.internal;

import java.sql.Timestamp;

import com.erytech.linktalk.dao.DaoFacade;
import com.erytech.linktalk.dao.domain.User;
import com.erytech.linktalk.service.IUserManager;

public class UserManager implements IUserManager {

	private DaoFacade daoFacade;

	public UserManager(DaoFacade daoFacade) {
		this.daoFacade = daoFacade;
	}

	public boolean hasUser(User user) {
		if(user.getName().toLowerCase().equals("all") || user.getName().toLowerCase().equals("所有人")) {
			return true;
		}
		return daoFacade.getUserDao().hasUser(user.getId());
	}
	
	public User login(User user) {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		user.setCreationTime(now);
		user.setLastPollTime(now);
		user.setLastPostTime(now);
		daoFacade.getUserDao().insertUser(user);
		daoFacade.getMessageDao().insertEnterRoomMessage(user);
		return daoFacade.getUserDao().getUser(user.getId());
	}

	public void logout(User user) {
		daoFacade.getUserDao().deleteUser(user.getId());
		daoFacade.getMessageDao().insertLeftRoomMessage(user);
	}

}
